package com.github.chxiaowu.common;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class Result<T> {

	private String message;
	private T result;
	private Integer code;

	public boolean isOk() {
		return ResultCode.SUCCESS.code() == code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public Result(ResultCode resultCode, String message, T result) {
		super();
		this.message = message;
		this.result = result;
		this.code = resultCode.code();
	}

	public Result(ResultCode resultCode, String message) {
		super();
		this.message = message;
		this.code = resultCode.code();
	}

	public Result(T result) {
		this.code = ResultCode.SUCCESS.code();
		this.result = result;
	}

	public Result() {
	}

	public static Result error(ResultCode code, String msg, Object result) {
		return new Result(code, msg, result);
	}

	public static Result error(ResultCode code, String msg) {
		return new Result(code, msg, null);
	}

	public static Result error(ResultCode code, Object result) {
		return new Result(code, code.desc(), null);
	}

	public static Result error(String msg, Object result) {
		return new Result(ResultCode.SERVER_ERROR, msg, result);
	}

	public static Result error(String msg) {
		return new Result(ResultCode.SERVER_ERROR, msg, null);
	}

	public static Result ok(ResultCode code, String msg, Object result) {
		return new Result(code, msg, result);
	}

	public static Result ok(ResultCode code, String msg) {
		return new Result(code, msg);
	}

	public static Result ok(String msg, Object result) {
		return new Result(ResultCode.SUCCESS, msg, result);
	}

	public static Result ok(Object result) {
		return new Result(ResultCode.SUCCESS, "", result);
	}

}